package com.alex.spring.annotation;/**
 * Created by Administrator on 2018\10\5 0005.
 */

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Administrator
 * @date 2018\10\5 0005
 */
public class Spring02 {

    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("Name : " + student.getName() );
        System.out.println("Age : " + student.getAge() );
    }
    @Test
    public void test02(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        TextEditor te = (TextEditor) context.getBean("textEditor");
        te.spellCheck();
    }
    @Test
    public void test03(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Profile profile = (Profile) context.getBean("profile");
        profile.printAge();
        profile.printName();
    }
    @Test
    public void test04(){
        AbstractApplicationContext context =
                new ClassPathXmlApplicationContext("ApplicationContext.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        context.registerShutdownHook();
    }
    @Test
    public void test05(){
        ApplicationContext ctx =
                new AnnotationConfigApplicationContext(HelloWorldConfig.class);
        HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
        helloWorld.setMessage("Hello World!");
        helloWorld.getMessage();
    }
    @Test
    public void test06(){
        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext();
        // ctx.register(HelloWorldConfig.class, OtherConfig.class);
        ctx.register(HelloWorldConfig.class);
        ctx.refresh();
        HelloWorld helloWorld  = ctx.getBean(HelloWorld.class);
        helloWorld.setMessage("Hello World!");
        helloWorld.getMessage();
    }
    @Test
    public void test07(){
        ApplicationContext ctx =
                new AnnotationConfigApplicationContext(TextEditorConfig.class);
        TextEditor te = ctx.getBean(TextEditor.class);
        te.spellCheck();
    }
    @Test
    public void test08(){
        ApplicationContext ctx =
                new AnnotationConfigApplicationContext(ConfigB.class);
        // now both beans A and B will be available...
        Bar a = ctx.getBean(Bar.class);
        Car b = ctx.getBean(Car.class);
    }
    @Test
    public void test09(){
        ConfigurableApplicationContext context =
                new ClassPathXmlApplicationContext("ApplicationContext.xml");
        context.start();
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        context.stop();
    }
    @Test
    public void test10(){
        ConfigurableApplicationContext context =
                new ClassPathXmlApplicationContext("ApplicationContext.xml");
        CustomEventPublisher cvp =
                (CustomEventPublisher) context.getBean("customEventPublisher");
        cvp.publish();
        cvp.publish();
    }
}
